class OtherError(Exception):
    """Simple exception to debug code in utils.py

    """
    pass


def flatten(l):
    new_l = []
    for elem in l:
        if isinstance(elem,list):
            new_l.extend(flatten(elem))
        else:
            new_l.append(elem)
    return new_l

def reverse(l):
    new_l = []
    for elem in l:
        new_l.insert(0,elem)
    return new_l
    

def list_sub_classes(moduleName,superClassName,spacing=25, collapse=1):
    """Prints the subclasses of superClassName that are flagged to 'displayInfo'


    """
    import sys
    module = sys.modules[moduleName]
    classes = []
    for elem in dir(module):
        is_sub_class = False

        try:
            is_sub_class = issubclass(getattr(module,elem),getattr(module,superClassName))
        except TypeError:
            pass
        except: raise

        if is_sub_class:
            classes.append(elem)            

    # TODO: print full __doc__ string.  And order by type.  List
    # 'basic' Vp first, then Vp using more and more complex templates,
    # etc.  Need an ordering of Vp that makes sense!
    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
    print "\n".join(
        [("* " + cls.ljust(spacing) +
          "\t" +
          processFunc(str(getattr(module,cls).__doc__))) for
         cls in classes if
         getattr(module,cls).displayInfo]
        )       
 

DEFAULT_SPACING=25
def list_instances(moduleName,className,spacing=DEFAULT_SPACING, collapse=1):
    """Prints the instances of className in moduleName that are flagged  to 'displayInfo'


    """
    import sys
    module = sys.modules[moduleName]
    names = dir(module)
    vps = map(lambda x: getattr(module,x),filter(lambda x:isinstance(getattr(module,x),getattr(module,className)), names))
    objects = filter(lambda x: x.displayInfo,vps)

    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
    print "\n".join(
        ["* " + str(object_).ljust(spacing) +
         "\t" +
         processFunc(object_.__doc__) for
         object_ in objects]
        )      



def list_instantiated_sub_classes(moduleName,superClassName,spacing=DEFAULT_SPACING, collapse=1):
    """Prints info about an instance of the subclasses of superClassName that are flagged to 'displayInfo'


    """
    import sys
    module = sys.modules[moduleName]
    classes = []
    for elem in dir(module):
        is_sub_class = False

        try:
            is_sub_class = issubclass(getattr(module,elem),getattr(module,superClassName))
        except TypeError:
            pass
        except: raise

        if is_sub_class:
            classes.append(elem)            

    # TODO: print full __doc__ string.  And order by type.  List
    # 'basic' Vp first, then Vp using more and more complex templates,
    # etc.  Need an ordering of Vp that makes sense!
    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
    objects = map(lambda x: getattr(module,cls)(),filter(lambda x: x.displayInfo,classes))
    print "\n".join(
        ["* " + str(object_).ljust(spacing) +
         "\t" +
         processFunc(object_.__doc__) for
         object_ in objects]
        )      
